
package com.rt.schedulenew.utils.util;

import java.awt.image.BufferedImage;

import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

import java.io.File;
import java.io.IOException;

import com.google.zxing.WriterException;
import com.google.zxing.oned.Code128Writer;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;
import java.util.Map;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.EncodeHintType;

import java.util.Hashtable;

import org.apache.commons.lang3.StringUtils;


/**
 * 二维码工具
 */
public class QrCodeUtil {
    public static String creatRrCode(String contents, int width, int height) {
        if (StringUtils.isBlank(contents)) {
            return "";
        }
        String binary = null;
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, (Map) hints);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            BufferedImage image = toBufferedImage(deleteWhite(bitMatrix));
            ImageIO.write(image, "png", out);
            byte[] bytes = out.toByteArray();
            BASE64Encoder encoder = new BASE64Encoder();
            binary = encoder.encodeBuffer(bytes).trim();
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e2) {
            e2.printStackTrace();
        }
        return binary;
    }

    public static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, 1);
        for (int x = 0; x < width; ++x) {
            for (int y = 0; y < height; ++y) {
                image.setRGB(x, y, matrix.get(x, y) ? -16777216 : -1);
            }
        }
        return image;
    }

    private static BitMatrix deleteWhite(BitMatrix matrix) {
        int[] rec = matrix.getEnclosingRectangle();
        int resWidth = rec[2] + 1;
        int resHeight = rec[3] + 1;
        BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
        resMatrix.clear();
        for (int i = 0; i < resWidth; ++i) {
            for (int j = 0; j < resHeight; ++j) {
                if (matrix.get(i + rec[0], j + rec[1])) {
                    resMatrix.set(i, j);
                }
            }
        }
        return resMatrix;
    }

//    public static void main(String[] args) {
//        String binary = creatRrCode("https://blog.csdn.net/ianly123", 100, 100);
//        System.out.println(binary);
//    }


    /**
     * 创建条码
     *
     * @param contents
     * @param width
     * @param height
     * @return
     */
    public static String creatBarCode(String contents, int width, int height) {
        if (StringUtils.isBlank(contents)) {
            return "";
        }
        String binary = null;
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        try {
            Code128Writer writer = new Code128Writer();
            // 编码内容, 编码类型, 宽度, 高度, 设置参数
            BitMatrix bitMatrix = writer.encode(contents, BarcodeFormat.CODE_128, width, height, hints);
            BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);

//            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.CODE_128, width, height, hints);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
//            BufferedImage image = toBufferedImage(deleteWhite(bitMatrix));
            ImageIO.write(image, "jpg", out);
            ImageIO.write(image, "jpg", new File("D:/a.jpg"));
            byte[] bytes = out.toByteArray();
            BASE64Encoder encoder = new BASE64Encoder();
            binary = encoder.encodeBuffer(bytes).trim()
                    .replace("\r\n", "");
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e2) {
            e2.printStackTrace();
        }
        return binary;
    }
}
